<?
include("includes/config.inc.php");
include("includes/auth.lib.php");
include("includes/function.inc.php");
include("includes/product_new.class.php");
$i = 0;
$j = 0;
$qry = "select id,fornitore from fornitore where giardino=1;";
$rs = $conn->Execute($qry);
$fornitori = Array();
while(!$rs->EOF) {
	$fornitori[$rs->Fields("id")] = $rs->Fields("fornitore");
	$rs->MoveNext();
}
$rs->Close();
$qry = "SELECT idarticolo,fornitore,costo,codice,produttore FROM articoli_new where giardino=1;";
/*
$qry = "SELECT idarticolo, idfornitore, (

SELECT costo
FROM articoli_new a
WHERE idarticolo = f.idarticolo
) AS costo
FROM `articoli_fornitore` f
WHERE idarticolo
IN (

SELECT idarticolo
FROM articoli_new a
WHERE costo >0
AND idarticolo = f.idarticolo
)
AND f.costo =0";
*/
$rs = $conn->Execute($qry);
while(!$rs->EOF) {
	/*
	$conn->Execute("update articoli_fornitore set costo=".$rs->Fields("costo")." where idarticolo=".$rs->Fields("idarticolo").
	" and idfornitore=".$rs->Fields("idfornitore").";");
	*/
	$qry = "select * from articoli_fornitore where idarticolo=".$rs->Fields("idarticolo");
	$rs_for = $conn->Execute($qry);
	if($rs_for->RecordCount()>0) {
		//if($rs_for->Fields("costo")==0) $qry_costo = ",costo=".$rs->Fields("costo");
		//if($rs_for->Fields("codice")!=$rs->Fields("codice")) $qry_codice = ",codice='".$rs->Fields("codice")."'";
		if($fornitori[$rs_for->Fields("idfornitore")]=="") {
		  $conn->Execute("delete from articoli_fornitore where idarticolo=".$rs_for->Fields("idarticolo")." and idfornitore=".$rs_for->Fields("idfornitore"));
		  $i++;
		}
		} else {
			if($rs->Fields("fornitore")!="") {
				$idfornitore = array_search($rs->Fields("fornitore"), $fornitori);
				if($idfornitore) {
					$qry = "insert into articoli_fornitore values(".$rs->Fields("idarticolo").",$idfornitore,".$rs->Fields("costo").",'".$rs->Fields("codice")."',1);";
					//die($qry);
					$conn->Execute($qry);
					$j++;
				}
			} elseif($rs->Fields("produttore")!="") {
				$idfornitore = array_search($rs->Fields("produttore"), $fornitori);
				if(!$idfornitore) {
					$conn->Execute("insert into fornitore values(NULL,'".addslashes($rs->Fields("produttore"))."',1)");
					$idfornitore = $conn->Insert_ID();
					$fornitori[$idfornitore] = $rs->Fields("produttore");
				}
				$qry = "insert into articoli_fornitore values(".$rs->Fields("idarticolo").",$idfornitore,".$rs->Fields("costo").",'".$rs->Fields("codice")."',1);";
				//die($qry);
				$conn->Execute($qry);
				$j++;
			} elseif($rs->Fields("codice")!="" || $rs->Fields("costo")!="") {
				$qry = "insert into articoli_fornitore values(".$rs->Fields("idarticolo").",0,".$rs->Fields("costo").",'".$rs->Fields("codice")."',1);";
				$conn->Execute($qry);
				$j++;
			}
	}
	$i++;
	$rs->MoveNext();
}
die("Aggiornati: $i;<br>Inseriti: $j");
/*
//$elenco = category::getChild(3,"0");
//die($elenco);
$qry = "SELECT hidden FROM articoli_new WHERE cid IN ($elenco) and giardino=1;";
die($qry);
//$qry = "select cid,url from tmp_url_categorie;";
$rs = esegui_queryRs($qry);
$arrCat1 = array();
$arrCat2 = array();
$i = 0;
while(!$rs->EOF) {
	//$arrCat1[$rs->Fields("cid")] = $rs->Fields("url");
	$permalink = strPermalink(utf8_encode($rs->Fields("titolo1")));
	//$conn->Execute("update articoli_new set hidden='".$permalink."' where idarticolo=".$rs->Fields("idarticolo")." and giardino=1;");
	//echo "update articoli_new set hidden='".$permalink."' where idarticolo=".$rs->Fields("idarticolo").";<br>";
	$rs->MoveNext();
	$i++;
}
$rs->Close();
die("TOT: $i");
$qry = "select cid,hidden as url from categorie_old;";
$rs = $conn->Execute($qry);
if($rs->RecordCount()>0) {
	$i = 0;
	$num = $rs->RecordCount();
	$error = "OK";
	while(!$rs->EOF) {
		$arrCat2[$rs->Fields("cid")] = $rs->Fields("url");
		//$permalink = strPermalink(utf8_encode($rs->Fields("titolo1")));
		//$conn->Execute("update articoli_new set hidden='".$permalink."' where idarticolo=".$rs->Fields("idarticolo").";");
		//echo "update articoli_new set hidden='".$permalink."' where idarticolo=".$rs->Fields("idarticolo").";<br>";
		/*
		if(!file_exists("imgs_prodotti/".$rs->Fields("idarticolo")."/".$rs->Fields("image_tmb")))
		  echo "imgs_prodotti/".$rs->Fields("idarticolo")."/".$rs->Fields("image_tmb")."<br>";
		if(!file_exists("imgs_prodotti/".$rs->Fields("idarticolo")."/".$rs->Fields("image")))
		  echo "imgs_prodotti/".$rs->Fields("idarticolo")."/".$rs->Fields("image")."<br>";
		if(!file_exists("imgs_prodotti/".$rs->Fields("idarticolo")."/".$rs->Fields("image_big")))
		  echo "imgs_prodotti/".$rs->Fields("idarticolo")."/".$rs->Fields("image_big")."<br>";
		 /*
		if(!file_exists("../immagini-piscine/".$rs->Fields("image_tmb")))
		  echo "../immagini-piscine/".$rs->Fields("image_tmb")."<br>";
		if(!file_exists("../immagini-piscine/".$rs->Fields("image")))
		  echo "../immagini-piscine/".$rs->Fields("image")."<br>";
		if(!file_exists("../immagini-piscine/".$rs->Fields("image_big")))
		  echo "../immagini-piscine/".$rs->Fields("image_big")."<br>";
		$rs->MoveNext();
	}
	$rs->Close();
}
foreach ($arrCat2 as $cid=>$url) {
	if($arrCat1[$cid]!=$arrCat2[$cid])
	  echo "$cid ".$arrCat2[$cid]."<br>";
}
*/
//die($error);
?>